package com.face.client.model;

import com.arcsoft.CLibrary;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.util.SDKUtils;

public interface Engine {

    Pointer get();

    /*sdk方法*/
    Pointer init();

    /*一般不调用*/
    void destroy();

    /*sdk方法*/
    void release(Pointer engine);

    default void releaseMem(Pointer memory) {
        SDKUtils.free(memory);
    }

    default boolean ifError(Pointer memory, NativeLong ret) {
        if (ret.longValue() != 0) {
            CLibrary.INSTANCE.free(memory);
            System.out.println(String.format("引擎初始化异常 ret 0x%x", ret.longValue()));
            return true;
        } else return false;
    }

    default void destroy(ThreadLocal<Pointer> pEngine, ThreadLocal<Pointer> pMemory) {
        Pointer engine = pEngine.get();
        if (engine != null) {
            release(engine);
        }
        Pointer memory = pMemory.get();
        if (memory != null) {
            releaseMem(memory);
        }
    }

    default Pointer get(ThreadLocal<Pointer> pEngine) {
        Pointer engine = pEngine.get();
        if (engine == null) {
            engine = init();
        }
        return engine;
    }
}
